import * as THREE from "three"
import resolutionEnum from "../enum/resolution.enum"
import sweepAlignmentEnum from "../enum/sweepAlignment.enum"
import sweepPlacementEnum from "../enum/sweepPlacement.enum"
import ObservableObject from "../observer/observable.object"

const l = {
    [resolutionEnum.BASE]: "512",
    [resolutionEnum.STANDARD]: "1k",
    [resolutionEnum.HIGH]: "2k",
    [resolutionEnum.ULTRAHIGH]: "4k"
  },
  c = {
    [resolutionEnum.BASE]: "low",
    [resolutionEnum.STANDARD]: "high",
    [resolutionEnum.HIGH]: "2k",
    [resolutionEnum.ULTRAHIGH]: "4k"
  }
export default class Sweep extends ObservableObject {
  skyboxes: any
  id: string
  index: number
  name: string
  position: THREE.Vector3
  floorPosition: THREE.Vector3
  floorId: string
  roomId: string
  rotation: THREE.Quaternion
  enabled: boolean
  vrenabled: boolean
  neighbours: any[]
  alignmentType: string
  placementType: string
  uuid: string
  resolutions: any
  constructor(e?: any) {
    super()
    this.skyboxes = {}
    if (e) {
      e.id && (this.id = e.id)
      this.index = e.index
      this.name = e.name || ""
      this.position = e.position || new THREE.Vector3()
      this.floorPosition = e.floorPosition || new THREE.Vector3()
      this.floorId = e.floorId
      this.roomId = e.roomId || null
      this.rotation = e.rotation || new THREE.Quaternion()
      this.enabled = e.enabled || !1
      this.vrenabled = e.vrenabled || !1
      this.neighbours = e.neighbours || []
      this.alignmentType = e.alignmentType || sweepAlignmentEnum.ALIGNED
      this.placementType = e.placementType || sweepPlacementEnum.UNPLACED
      this.uuid = e.uuid
    }
  }
  isAligned() {
    return this.alignmentType === sweepAlignmentEnum.ALIGNED
  }
  isUnplaced() {
    return this.placementType === sweepPlacementEnum.UNPLACED
  }
  get sid() {
    return this.id
  }
  equals(e: Sweep) {
    return (
      this.id === e.id &&
      this.index === e.index &&
      this.name === e.name &&
      this.position.equals(e.position) &&
      this.floorId === e.floorId &&
      this.roomId === e.roomId &&
      this.rotation.equals(e.rotation) &&
      this.enabled === e.enabled &&
      this.vrenabled === e.vrenabled &&
      this.alignmentType === e.alignmentType &&
      this.placementType === e.placementType
    )
  }
  copy(e: Sweep) {
    this.id = e.id
    this.index = e.index
    this.name = e.name
    this.position = new THREE.Vector3().copy(e.position)
    this.floorPosition = new THREE.Vector3().copy(e.floorPosition)
    this.floorId = e.floorId
    this.roomId = e.roomId
    this.rotation = new THREE.Quaternion().copy(e.rotation)
    this.enabled = e.enabled
    this.vrenabled = e.vrenabled
    this.neighbours = e.neighbours.slice()
    this.alignmentType = e.alignmentType
    this.placementType = e.placementType
    this.resolutions = e.resolutions
    this.skyboxes = Object.assign({}, e.skyboxes)
    return this
  }
  refresh(e: Sweep) {
    this.resolutions = e.resolutions
    for (const t in this.skyboxes) {
      const i = t
      this.skyboxes[i].refreshFrom(e.skyboxes[i])
    }
  }
  async getTileUrl(e: string, t, i, n) {
    const s = await this.getSkybox(e)
    let r: string = s ? s.tileUrlTemplate : `tiles/${this.id}/${l[e]}_face<face>_<x>_<y>.jpg`
    return r.replace("<face>", `${t}`).replace("<x>", `${i}`).replace("<y>", `${n}`)
  }
  async getFaceUrl(e: string, t: number) {
    const i = await this.getSkybox(e)
    let n = i ? i.urlTemplate : `pan/${c[e]}/${this.id}_skybox${t}.jpg`
    return n.replace("<face>", `${t}`) as string
  }
  async getSkybox(e: string) {
    const t = this.skyboxes[e]
    return t ? t.get() : t
  }
}
